define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, NewtestDetail, $ionicSlideBoxDelegate, GoPage, GoDialog, GoShare,GoWapAuth, $window, $timeout, $stateParams, $ionicModal, Auth, $interval, $ionicPopup) {
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
//			$scope.img = "";
//			$scope.text = "";
			$scope.set_time = false;
			$scope.goods_id = $stateParams.goods_id;
			$scope.product_id = $stateParams.product_id;
			$scope.act_id = $stateParams.act_id;
			$scope.init_data();
		}, false);
		
		$scope.$on('$ionicView.afterLeave', function() {
			$scope.closeGuidShare();
		}, false);
		
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
		
		//初始化页面
		$scope.init_data = function() {
			NewtestDetail.product_detail($scope.act_id,$scope.goods_id,$scope.product_id, function(data) {
				if(!data.success){
					GoDialog.alert("提示",data.msg,function(){
						GoPage.go_back();
					})
					return;
				}
				console.log(data)
				if(data.result) {
					$scope.goods_num = 1;
					if(data.result.goods.min_buy != null && data.result.goods.min_buy != '0'){//默认购买数量
						$scope.goods_num = parseInt(data.result.min_buy);
					}
					$scope.datas = data.result;
					$scope.detail = data.result.goods
					$scope.init_time(data.result, data.process_time);
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
					$scope.go_share_wx();
				}
			});
		}
		
		/************提交表单***************/
		$scope.submit = function() {
			if($scope.btn_type == 1) {
				NewtestDetail.test_apply($scope.act_id, $scope.detail.goods_id, $scope.goods_num, $scope.product_id, function(data) {
					if(data.success == true) {
						$scope.btn_type = 5;
						$scope.btn_name = '等待审核';
//						$scope.img = 'img/waiting.png';
						GoDialog.alert("提示","你的申请已成功提交，等待系统审核！");
					} else {
						GoDialog.alert("提示",data.msg);
					}
				})
			} else if($scope.btn_type == 3) {
				GoPage.go_states('buyerpayment', {
					"order_id": $scope.datas.order.order_id
				});
			}
		}
		/************提交表单***************/
		$scope.init_time = function(data, now_time) {
			if(now_time < data.start_time) {
				$scope.btn_type = 5;
				$scope.btn_name = '未开始';
				$scope.act_time_name = '距开始';
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.start_time - now_time;
			} else {
				$scope.act_time_name = '距结束';
				$scope.init_btn(now_time);
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.end_time - now_time;
			}
			data.formate_time = $scope.formate_time(data.now_time);
			$scope.set_timer(now_time);
		};
		$scope.init_btn = function(now_time) {
			if(now_time >= $scope.datas.start_time && now_time <= $scope.datas.end_time && $scope.datas.aGoods.activity_info.act_open == 'true') {
				if($scope.detail.store > 0 || $scope.detail.aGoods.nostore_sell) {
					if(!$scope.datas.is_apply && now_time <= $scope.datas.apply_end_time) {
						$scope.btn_type = 1;
						$scope.btn_name = '立即申请';
					} else {
						if($scope.datas.is_apply) {
							if($scope.datas.order) {
								if($scope.datas.order.status == 'finish' || $scope.datas.order.pay_status == '1') {
									$scope.btn_type = 3;
									$scope.btn_name = '查看订单详情';
//									$scope.text = "感谢您使用了我们的产品，请在使用后到订单一栏上传调查问卷，有机会抽取免单机会哦！";
									GoDialog.alert("提示","感谢您使用了我们的产品，请在使用后到订单一栏上传调查问卷，有机会抽取免单机会哦！");
								} else {
									$scope.btn_type = 3;
									$scope.btn_name = '去订单支付';
//									$scope.img = 'img/ok.png';
									GoDialog.alert("提示","恭喜！您已被选为测试用户，订单已发送，请及时付款。");
								}
							} else {
								if(now_time < $scope.datas.apply_end_time) {
									$scope.btn_type = 5;
									$scope.btn_name = '等待审核';
//									$scope.img = 'img/waiting.png';
									GoDialog.alert("提示","你的申请已成功提交，等待系统审核！");
								} else {
//									$scope.img = 'img/yihan.png';
									GoDialog.alert("提示","真遗憾！您的申请未通过，请再接再厉。");
									$scope.btn_name = '立即申请';
									$scope.btn_type = 0;
								}
							}
						}else{
							$scope.btn_name = '立即申请';
//							$scope.text = "申请已经结束";
							GoDialog.alert("提示","申请已经结束");
							$scope.btn_type = 0;
						}
					}
				} else {
					$scope.btn_type = 0;
					$scope.btn_name = '已售完';
				}
			} else if(now_time < $scope.datas.start_time) {
				$scope.btn_type = 0;
				$scope.btn_name = '未开始';
			} else {
				if($scope.datas.order && $scope.datas.lottery_status == 'true') {
//					$scope.img = 'img/free.png';
					GoDialog.alert("提示","恭喜！您已获得免单，请等待商家与您联系。");
					$scope.btn_type = 3;
					$scope.btn_name = '查看订单详情';
				} else if($scope.datas.order) {
//					$scope.text = "很遗憾！您没能获得免单！谢谢参与！"
					GoDialog.alert("提示","很遗憾！您没能获得免单！谢谢参与！");
					$scope.btn_name = '已结束';
					$scope.btn_type = 0;
				}
			}
		}
		$scope.set_timer = function(now_time) {
			if(!$scope.set_time) {
				$scope.set_time = true;
			} else {
				return;
			}
			$interval(function() {
				if($scope.datas.now_time != undefined) {
					$scope.datas.now_time = $scope.datas.now_time - 1;
					if($scope.datas.now_time <= 0) {
						if(now_time >= $scope.datas.start_time) {
							$scope.datas.now_time = $scope.datas.end_time - now_time;
							$scope.act_time_name = '距结束';
							$scope.init_btn(now_time);
						}
					}
				} else {
					if(now_time >= $scope.datas.start_time) {
						$scope.datas.now_time = $scope.datas.end_time - now_time;
						$scope.act_time_name = '距结束';
						$scope.init_btn(now_time);
					} else {
						$scope.datas.now_time = $scope.datas.start_time - now_time;
						$scope.act_time_name = '距开始';
					}
				}
				if(now_time > $scope.datas.apply_end_time) {
					$scope.btn_type = 5;
					$scope.btn_name = '申请结束';
				}
				now_time++;
				$scope.datas.formate_time = $scope.formate_time($scope.datas.now_time, now_time);
			}, 1000)
		};
		//计算时间
		$scope.formate_time = function(time,now_time) {
			var timeArr = [0,0,0,0];
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
//				return day + '天' + hour + '小时' + min + '分' + second + '秒';
				timeArr[0] = day;
				timeArr[1] = hour;
				timeArr[2] = min;
				timeArr[3] = second;
			} else {
				$scope.btn_type = 5;
				$scope.btn_name = '已结束';
//				return '0天0小时0分0秒';
			}
			return timeArr;
		};
		
		//跳转到首页
		$scope.onshow_first = function() {
			GoPage.go_states("first", {"store_id": $scope.detail.store_id});
		};
		
		//跳转到商品详情
		$scope.onshow_productInfo = function() {
			GoPage.go_states("activity_info", {
				"goods_id": $scope.goods_id,
				"act_id": $scope.act_id,
				"product_id": $scope.product_id,
				"type":"5"
			});
		}
		
		//上拉跳转到商品详情
		$scope.isLoad = false;
		$scope.doRefreshMore = function(){
			$scope.isLoad = true;
			$scope.$broadcast('scroll.infiniteScrollComplete');
			$timeout(function() {
				$scope.isLoad = false;
			}, 3000);
			$scope.onshow_productInfo();
		}
		
		
		/***********显示《客服》**********/
		$ionicModal.fromTemplateUrl("templates/modal/customer_service.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_customer = modal;
		});
		//打开
		$scope.openModal_customer = function() {//客服
			NewtestDetail.contact_show($scope.detail.store_id, function(data) {
				if(!data||data.cusphone.length<1){
					GoDialog.alert("提示","店家暂时还没有设置客服哦");
					return;
				}
				$scope.customerList = data;
				$scope.modal_customer.show();
			})
		}
		//隐藏
		$scope.closeModal_customer = function() {
			$scope.modal_customer.hide();
		}
		//选择客服
		$scope.selectCustomer = function(index){
			$scope.customerList_index = index;
		}
		//联系客服
		$scope.do_tel = function() {
			$scope.closeModal_customer();
			if(!$scope.customerList_index) return;
			var param = $scope.customerList.cusphone[$scope.customerList_index];
			if(param.type == 2) {
				$window.location.href = "tel:" + param.value;
			} else if(param.type == 1) {
				if(param.isonline == '0') {
					GoPage.go_txcall3(param.id, param.code, param.vvckey);
				} else {
					GoPage.go_txcall2($scope.detail.store_id, $scope.detail.store_id, param.id, param.code, param.vvckey);
				}
			}
		}
		/***********显示《客服》**********/
		
		/***************活动规则**************/
		$ionicModal.fromTemplateUrl("templates/modal/activityRule.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_rule = modal;
		});
		//打开
		$scope.openModal_rule = function() {
			$scope.detail.rule = $scope.datas.rule;//字段转换一下
			$scope.modal_rule.show();
		}
		//隐藏
		$scope.closeModal_rule = function() {
			$scope.modal_rule.hide();
		}
		/***************活动规则**************/
		
		/***************分享**************/
		$ionicModal.fromTemplateUrl("templates/modal/keyshare_modal.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_share = modal;
		});
		//打开
		$scope.openModal_share = function() {
			if($scope.openGuidShare()) {
				$scope.modal_share.show();
			}
		}
		//隐藏
		$scope.closeModal_share = function() {
			$scope.modal_share.hide();
		}
		
		//分享（自定义每个页面的参数）
		$scope.go_share = function(index) {
			var param = new Object();
			if($scope.detail) {
				param.title = $scope.detail.name ? $scope.detail.name : undefined;
				param.desc = $scope.detail.brief ? $scope.detail.brief : undefined;
				param.thumb = $scope.detail.title_images ? $scope.detail.title_images[0].image : undefined;
				param.link = 'activitythree/'+$scope.act_id+'/'+$scope.goods_id +'/'+$scope.product_id;
			}
			GoShare.do_share(param, index);
		}
		
		//分享（自定义每个页面的参数）
		$scope.go_share_wx = function() {
			//分享打开
			GoWapAuth.execisoff(false);
			//设置分享
			var param = new Object();
			if($scope.detail) {
				param.title = $scope.detail.name ? $scope.detail.name : undefined;
				param.desc = $scope.detail.brief ? $scope.detail.brief : undefined;
				param.thumb = $scope.detail.title_images ? $scope.detail.title_images[0].image : undefined;
				param.link = 'activitythree/'+$scope.act_id+'/' +$scope.goods_id+'/'+$scope.product_id;
			}
			GoWapAuth.execshare(param);
		}
		/***************分享**************/
		
		
	}

	controller.$inject = ['$scope', 'NewtestDetail', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', 'GoShare','GoWapAuth', '$window', '$timeout', '$stateParams', '$ionicModal', 'Auth', '$interval','$ionicPopup'];

	return app.registerController("newtest_detailctrl", controller);
});